package com.gamer.p1.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.micrometer.common.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;


public class JsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        //序列化的时候序列对象的所有属性
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        //反序列化的时候如果多了其他属性,不抛出异常
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //如果是空对象的时候,不抛异常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 将泛型对象实例obj序列化成String
     *
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> String serializeToString(T obj) {
        String result;
        try {
            if (obj == null) {
                return null;
            }
            result = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("serialize2String exception: " + e.getMessage());
        }
        return result;
    }

    /**
     * 将String反序列化为对应的对象实例
     *
     * @param originStr
     * @param classT
     * @param <T>
     * @return
     */
    public static <T> T deserializeFromString(String originStr, Class<T> classT) {
        T result;
        try {
            if (StringUtils.isBlank(originStr)) {
                return null;
            }
            result = objectMapper.readValue(originStr, classT);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("deserializeFromString exception: " + e.getMessage());
        }
        return result;
    }

    /**
     * 将String反序列化为对应的对象实例
     *
     * @param originStr
     * @param classT
     * @return
     */
    public static <T> T deserializeFromString(String originStr, TypeReference<T> classT) {
        T result;
        try {
            if (StringUtils.isBlank(originStr)) {
                return null;
            }
            result = objectMapper.readValue(originStr, classT);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("deserializeFromString exception: " + e.getMessage());
        }
        return result;
    }

    /**
     * 将InputStream反序列化为对应的对象实例
     *
     * @param inputStream
     * @param classT
     * @param <T>
     * @return
     */
    public static <T> T deserializeFromStream(InputStream inputStream, Class<T> classT) {
        T result;
        try {
            result = objectMapper.readValue(inputStream, classT);
        } catch (IOException e) {
            throw new RuntimeException("deserializeFromStream exception: " + e.getMessage());
        }
        return result;
    }
}
